package com.yang.booking.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yang.booking.config.JwtTokenConfig;
import com.yang.booking.dao.UserMapper;
import com.yang.booking.pojo.Student;
import com.yang.booking.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtTokenConfig jwtTokenConfig;

    public Map<String, Object> insertUser(String storeId, String name){

        User user = new User();
        user.setStoreId(storeId);
        user.setName(name);
        user.setCreateAt(new Date());
        user.setUpdateAt(new Date());
        userMapper.insert(user);

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "插入成功");
        msg.put("code", 200);
        return msg;
    }

    public Map<String, Object> sign_in(String username, String password){

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username).eq("password", password);
        User user = userMapper.selectOne(wrapper);

        if (user != null && user.getUsername().equals(username) && user.getPassword().equals(password)){
            String token = jwtTokenConfig.generateToken(user);
            Map<String, Object> msg = new HashMap<String,Object>();
            msg.put("massage", "登录成功");
            msg.put("code", 200);
            msg.put("token", token);
            return msg;
        }

        Map<String, Object> msg = new HashMap<String,Object>();
        msg.put("massage", "登录失败,账号或密码错误");
        msg.put("code", 400);
        msg.put("token", null);
        return msg;
    }
}
